const app = getApp();
var common=require("../../common.js")
var timer=null;
Page({
  onShow: function () {
    this.pause = false;
    this.initscene();
   }, 
  pause:false,
  animation: null,
  jid:0,
  data: {
    show: 1,
    albumShow: ['none', 'none', 'none', 'none', 'none', 'none'],
    selectedIndex:-1,
    selectedUid:0,
    targetUid:0,
    bigImg:'/image/2.jpg',
    showModalStatus:false,    
    gid:0,
    nowScore:0,
    historyScore:0
  },
  onShareAppMessage: function () {
    return {
      title: '厉害了，找到"' + this.data.detail.target + '",还有机会赢取红包奖励',
      imageUrl: '../../image/f.jpg',
      path: 'pages/suren/begin?id='+this.data.gid
    }
  },
  onLoad: function (params) {
    console.log(typeof params)
    if (typeof params.id=='undefined'){
        wx.reLaunch({
          url: '../../pages/index/index',
        })
    }
    this.data.gid = params.id;   
    this.startSurenGame();
    
  },
  startCountDown:function(){
    this.setData({
      num: app.globalData.timeLimit
    })
	this.pause=false;
    this.countdown(this);
  },
  countdown:function(that){
	if(this.pause)return false;
    timer=setTimeout(function () {
      if(that.data.num<=0){
        that.submitResult(1);
        return;
      }
      that.setData({num:that.data.num-1})
      that.countdown(that);
    }, 1000);
  },
  selectPhoto:function(e){//选中图片
    var index=parseInt(e.currentTarget.dataset.id);
    var reset=false
    if (this.data.albumShow[index] == "none") {
      reset=true      
    }else{
      this.data.selectedIndex = -1;
      this.data.selectedUid=0;
    }

    this.data.albumShow = ['none', 'none', 'none', 'none', 'none', 'none'];
    if(reset){
      this.data.albumShow[index] = "block";
      this.data.selectedIndex = index;
    }
    this.setData({
      albumShow: this.data.albumShow
    })
  },
  //显示大图
  showBigImg: function (e) {
    console.log(e);
    var currentStatu = e.currentTarget.dataset.statu;
    if (typeof currentStatu =='undefined'){
      
      if(e.currentTarget.id=='avatar'){
        this.setData({
          bigImg: e.currentTarget.dataset.src
        })
      }else{
        var index = parseInt(e.currentTarget.dataset.id);
        this.setData({
          bigImg: this.data.album[index].album
        })
      } 
      this.util('open')

    }else{
      this.util(currentStatu)
    }
  },
  //显示大图的动画效果
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例
    var animation = wx.createAnimation({
      duration: 200, //动画时长
      timingFunction: "linear", //线性
      delay: 0 //0则不延迟
    });

    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;

    // 第3步：执行第一组动画
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })

      //关闭
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)

    // 显示
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },
  getRand:function(){
    return Math.ceil(Math.random()*10000);
  },
  processTeam:function(data){
    var that=this;
    for (var j = 0, len = data.length; j < len; j++) {

      if (typeof data[j].headPic !== "undefined") {
        that.data.targetUid = data[j].uid;
        that.setData({
          avatarUrl: data[j].headPic
        });
        break;
      }
    }
    that.data.album = data;
    that.data.albumShow = ['none', 'none', 'none', 'none', 'none', 'none'];
    that.setData({
      album: that.data.album,
      albumShow: that.data.albumShow
    });    
  },
  startSurenGame:function(){    
    var that=this;
	wx.showLoading({
      title: '正在获取游戏',
	  mask:true,
    })
    common.request({
      url: 'c=Game&a=startSurenGame',
      data:{gid:that.data.gid},
      success: function (e) {
        if(e.status!=0){
          wx.showModal({
            title: '错误',
            content: e.msg,
            complete: function () {
              wx.navigateTo({
                url: '../../pages/index/index',
              })
            }
          })
          return;
        }
        that.jid=e.jid;
        that.setData({
          target:e.target,
          relay:e.relay,
          avatarUrl:e.pic,
          });
        that.processTeam(e.list); 
        if (e.relay != 1) {//禁止转发
          wx.hideShareMenu({});
        }
      },
    complete:function(e){
		  wx.hideLoading();
	  },
	  method: "POST"
    });
  },
    /**
 * 生命周期函数--监听页面隐藏
 */
  onHide: function () {
      this.pauseTimer();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.pauseTimer();
  },
  pauseTimer:function(){
	clearTimeout(timer);
    this.pause=true;
  },
  submitResult: function (isAuto) {
    if (isAuto != 1 && this.data.selectedIndex<0){
		  wx.showToast({
			image:'../../image/warn.png',
            title: '请选择目标',
          })
		  return;
	}
	
    this.pauseTimer();
    var that = this;
    wx.showLoading({
      title: '正在提交',
	  mask:true,
    })
    common.request({
      url: 'c=Game&a=submitSurenResult',
      data: {
        gid: that.data.gid,
        pic: that.data.album[this.data.selectedIndex],
        jid: that.jid,
      },
      success: function (e) { 
        if(e.status!=0){
          wx.showToast({
            title: e.msg,
            icon:'none'
          })
          return;
        }
        if(e.result==1){     
          var tip='';    
          if(e.prize==0){
            tip = '好可惜，红包奖励已经被抢完了';
          }else{
            tip = '获得' + e.prize + '元奖励,可到个人中心提现';
          }
          if (e.return != '0') {
            tip = ',您的保证金已原路退回';
          }
          that.setData({
            tipImg: '../../image/face_suc.jpg',
            tip1: '找对了，真厉害！',
            tip2: tip,
            showTip: true,
            show: true
          });
        }else{
          that.setData({
            tipImg: '../../image/face_fail.jpg',
            tip1: '真可惜，没有找对唷',
            tip2: '我与红包的距离只差0.01米',
            showTip: true,
            show: true
          }); 
        }       
        
      },
      complete: function (ret){ 
        wx.hideLoading();
      },
      method: "POST"
    });
  },
  initscene: function () {
    var smallWait = 300;
    var beginScale = 0.01;
    var beginPos = 150;
    var endPos = 250;
    var disPos = 300;
    var start = 500;
    const appearTime = 300;
    const disappearTime = 500;
    const stayTime = 700;

    //第1步：创建动画实例
    var animation = wx.createAnimation({
      duration: 10, //动画时长
      timingFunction: "linear", //线性
      delay: 0
    });
    this.animation = animation;

    this.animation.opacity(0).top(beginPos).scale(beginScale).step();
    this.setData({
      beginAnimationData3: this.animation.export()
    })
    start += smallWait

    setTimeout(function () {
      this.animation.opacity(1).top(endPos).scale(1).step({ timingFunction: "linear", duration: appearTime });//
      this.setData({
        beginAnimationData3: this.animation.export()
      })
    }.bind(this), start)
    start += appearTime + stayTime
    //return false;

    setTimeout(function () {
      this.animation.opacity(0).top(disPos).scale(13.5).step({ timingFunction: "linear", duration: disappearTime });//
      this.setData({
        beginAnimationData3: this.animation.export()
      })
    }.bind(this), start)
    start += disappearTime + 100

    /////////////////////////2222222222///////////////////////////////////////////////////////////////////////////


    setTimeout(function () {
      this.animation.opacity(0).top(beginPos).scale(beginScale).step();
      this.setData({
        beginAnimationData2: this.animation.export()
      })
    }.bind(this), start)
    start += smallWait


    setTimeout(function () {
      this.animation.opacity(1).top(endPos).scale(1).step({ timingFunction: "linear", duration: appearTime });//
      this.setData({
        beginAnimationData2: this.animation.export()
      })
    }.bind(this), start)
    start += appearTime + stayTime
    //return false;


    setTimeout(function () {
      this.animation.opacity(0).top(disPos).scale(13.5).step({ timingFunction: "linear", duration: disappearTime });//
      this.setData({
        beginAnimationData2: this.animation.export()
      })
    }.bind(this), start)
    start += disappearTime
    /////////////////////////////11111111///////////////////////////////////////////////

    setTimeout(function () {
      this.animation.opacity(0).top(beginPos).scale(beginScale).step({ duration: 10 });
      this.setData({
        beginAnimationData1: this.animation.export()
      })
    }.bind(this), start)
    start += smallWait


    setTimeout(function () {
      this.animation.opacity(1).top(endPos).scale(1).step({ timingFunction: "linear", duration: appearTime });
      this.setData({
        beginAnimationData1: this.animation.export()
      })
    }.bind(this), start)
    start += appearTime + stayTime

    //放大并消失
    setTimeout(function () {
      this.animation.opacity(0).top(disPos).scale(13.5).step({ timingFunction: "linear", duration: disappearTime });
      this.setData({
        beginAnimationData1: this.animation.export()
      })
    }.bind(this), start)

    start += disappearTime
    ////////////////////////////////////////////////////
    start += 500
    setTimeout(function () {
      this.animation.opacity(0).step({ duration: 1500 });
      this.setData({
        animationModal: this.animation.export()
      })
      this.startCountDown();
    }.bind(this), start)
    start += 500
    /////////////////////////////////////

    setTimeout(function () {
      this.setData({
        show: 0
      });

    }.bind(this), start)

  },
  createTap: function () {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  homeTap: function () {
    wx.reLaunch({
      url: '../suren/home',
    })
  },

});

